//
//  GJPhotosSelectView.m
//  YDGJ
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "GJPhotosSelectView.h"
#import "FD_Method.h"
#import "GJAlertView.h"

@interface GJPhotosSelectView ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSArray *_cameraTitle;
}

@property(nonatomic,copy)didSelectedBlock callBackBlock;
@property(nonatomic,strong)UINavigationController *navControl;


@end

@implementation GJPhotosSelectView

+(instancetype)photoSelectView
{
    return [[self alloc]initWithFrame:CGRectZero];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        NSMutableArray *cameraArr = [NSMutableArray arrayWithCapacity:1];
        if ([FD_Method isCameraAvailable])
        {
            [cameraArr addObject:GJPhotograph];
        }
        if ([FD_Method isPhotoLibraryAvailable]) {
            [cameraArr addObject:GJCameraLibrary];
        }
        _cameraTitle = cameraArr;
        cameraArr = nil;
        self.hidden = YES;
       
        
    }
    return self;
}

-(void)dealloc
{
    NSLog(@"释放%s",__func__);
}

-(void)openPhotoPickerWithNavControl:(UINavigationController *)navControl  handCallBlcok:(didSelectedBlock)block
{
    if (!_cameraTitle.count)
    {
        SystemAlertView(@"设备无摄像头", @"", @"确定");
        return;
    }
    if (nil == navControl) return;
    
    __weak typeof(NSArray *) tempCamera = _cameraTitle;
    WEAKSELF
    [GJAlertView actionSheetBCallBlock:^(NSInteger buttonIndex) {
        
        UIImagePickerController *imgPickerVC = [[UIImagePickerController alloc] init];
        if ([tempCamera[buttonIndex - 1] isEqualToString:GJPhotograph]) {
            imgPickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
        }else{
            imgPickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        
        imgPickerVC.allowsEditing = YES;
        imgPickerVC.delegate = weakSelf;
        [FD_Method presentNavBarStyle];
        [weakSelf.navControl presentViewController:imgPickerVC animated:YES completion:NULL];

        
    } cancelBlock:^{
        [weakSelf removeFromSuperview];
    } WithTitle:nil cancelButtonTitle:@"取消" otherButtonTitleArr:_cameraTitle];
    
    self.callBackBlock = block;
    self.navControl = navControl;
}

#pragma mark - =====UIImagePickerController Delegate=====
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    UIImage *image= [info objectForKey:@"UIImagePickerControllerEditedImage"];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    NSLog(@"照片压缩前大小：%.2fMB",(CGFloat)imageData.length/(1024*1024));

    if (imageData && self.callBackBlock)
    {
        self.callBackBlock(imageData);
    }
    
    [self removeFromSuperview];
    [FD_Method dismissRestoreNavBarStyle];
    [picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self removeFromSuperview];
    [FD_Method dismissRestoreNavBarStyle];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

@end
